package com.jwsoft.manager.core.integration.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.AuthStatusEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.data.core.util.PagerUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.enums.EnrollPreTypeEnum;
import com.jwsoft.manager.common.enums.SaveTypeEnum;
import com.jwsoft.manager.common.vo.eduBusiEnrollOrder.EduBusiEnrollOrderQueryVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollPre.EduStudentEnrollPreVO;
import com.jwsoft.manager.core.dao.model.EduStudentEnrollPre;
import com.jwsoft.manager.core.dao.service.EduBusiEnrollOrderService;
import com.jwsoft.manager.core.dao.service.EduStudentEnrollPreService;
import com.jwsoft.manager.core.integration.EduBusiEnrollOrderIntegration;
import com.jwsoft.manager.core.integration.EduStudentEnrollPreIntegration;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.convert.Convert;
import java.util.List;
import com.bifang.engine.assistant.core.exception.AppException;
import org.springframework.util.ObjectUtils;
import org.springframework.transaction.annotation.Transactional;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.jwsoft.manager.common.vo.eduStudentEnrollPreOffline.EduStudentEnrollPreOfflineVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollPreOffline.EduStudentEnrollPreOfflineKeyVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollPreOffline.EduStudentEnrollPreOfflineQueryVO;
import com.jwsoft.manager.core.integration.EduStudentEnrollPreOfflineIntegration;
import com.jwsoft.manager.core.dao.service.EduStudentEnrollPreOfflineService;
import com.jwsoft.manager.core.dao.model.EduStudentEnrollPreOffline;
/**
 * 线下新生登记业务实现类
 *
 * @author wangt
 * @since 2024-06-06
 */
@ApiService(funcCode = "eduStudentEnrollPreOffline", title = "线下新生登记")
public class EduStudentEnrollPreOfflineIntegrationImpl implements EduStudentEnrollPreOfflineIntegration {

    @Autowired
    public EduStudentEnrollPreOfflineService eduStudentEnrollPreOfflineService;

    @Autowired
    private EduStudentEnrollPreService eduStudentEnrollPreService;

    @Autowired
    private EduBusiEnrollOrderIntegration eduBusiEnrollOrderIntegration;

    @Autowired
    private EduStudentEnrollPreIntegration eduStudentEnrollPreIntegration;

    @Override
    @OpApi(funcCode = "eduStudentEnrollPreOffline0001", title = "线下新生登记分页查询", funcType = FuncTypeEnum.query,checkPrivilege = BoolEnum.FALSE)
    public PageInfo<EduStudentEnrollPreOfflineVO> getList(EduStudentEnrollPreOfflineQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        QueryWrapper<EduStudentEnrollPreOffline> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(StringUtils.isNotBlank(vo.getAuthStatus()),EduStudentEnrollPreOffline::getAuthStatus, vo.getAuthStatus())
                .eq(StringUtils.isNotBlank(vo.getIdcard()),EduStudentEnrollPreOffline::getIdcard, vo.getIdcard());
        List<EduStudentEnrollPreOffline> list = eduStudentEnrollPreOfflineService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduStudentEnrollPreOffline> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduStudentEnrollPreOfflineVO> resultList = Convert.toList(EduStudentEnrollPreOfflineVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollPreOffline0002", title = "线下新生登记根据主键查询详情", funcType = FuncTypeEnum.query,checkPrivilege = BoolEnum.FALSE)
    public EduStudentEnrollPreOfflineVO getById(EduStudentEnrollPreOfflineKeyVO vo) {
        EduStudentEnrollPreOffline entity = eduStudentEnrollPreOfflineService.getById(vo.getStudentEnrollPreId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduStudentEnrollPreOfflineVO result = Convert.convert(EduStudentEnrollPreOfflineVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "eduStudentEnrollPreOffline0003", title = "线下新生登记保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduStudentEnrollPreOfflineVO vo) {
        //主键为空为新增校验
        if(StringUtils.isBlank(vo.getIdcard())){
            throw new AppException("学生身份证不能为空");
        }

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollPreOffline0004", title = "线下新生登记新增", funcType = FuncTypeEnum.insert,publicFlag = BoolEnum.TRUE)
    @SaveOplog(
            operationType = "eduStudentEnrollPreOffline0004",
            operationName = "线下新生登记新增",
            dataType = "studentEnrollPreId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduStudentEnrollPreOfflineVO add(EduStudentEnrollPreOfflineVO vo) {
        //保存前校验
        checkSave(vo);

        QueryWrapper<EduStudentEnrollPre> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EduStudentEnrollPre::getIdcard,vo.getIdcard());
        List<EduStudentEnrollPre> enrollPreList = eduStudentEnrollPreService.list(queryWrapper);
        if(!CollectionUtils.isEmpty(enrollPreList)){
            throw new AppException(vo.getIdcard()+"该身份证已办理预采集登记！");
        }

        QueryWrapper<EduStudentEnrollPreOffline> queryWrapperOffline = new QueryWrapper<>();
        queryWrapperOffline.lambda().eq(EduStudentEnrollPreOffline::getIdcard,vo.getIdcard());
        List<EduStudentEnrollPreOffline> enrollPreOfflineList = eduStudentEnrollPreOfflineService.list(queryWrapperOffline);
        if(!CollectionUtils.isEmpty(enrollPreOfflineList)){
            throw new AppException(vo.getIdcard()+"该身份证已办理线下登记，请不要重复登记！");
        }


        vo.setAuthStatus(AuthStatusEnum.NODO.getType());

        //保存数据

        EduStudentEnrollPreOffline entity = Convert.convert(EduStudentEnrollPreOffline.class, vo);
        eduStudentEnrollPreOfflineService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setStudentEnrollPreId(entity.getStudentEnrollPreId());

        EduBusiEnrollOrderQueryVO orderQueryVO = new EduBusiEnrollOrderQueryVO();
        orderQueryVO = Convert.convert(EduBusiEnrollOrderQueryVO.class, vo);
        String orderNum = eduBusiEnrollOrderIntegration.appDistributeOrderNum(orderQueryVO);
        vo.setOrderNum(orderNum);
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollPreOffline0005", title = "线下新生登记修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduStudentEnrollPreOffline0005",
            operationName = "线下新生登记修改",
            dataType = "studentEnrollPreId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduStudentEnrollPreOfflineVO edit(EduStudentEnrollPreOfflineVO vo) {
        if (ObjectUtils.isEmpty(vo.getStudentEnrollPreId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduStudentEnrollPreOffline oldEntity = eduStudentEnrollPreOfflineService.getById(vo.getStudentEnrollPreId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduStudentEnrollPreOffline entity = Convert.convert(EduStudentEnrollPreOffline.class, vo);
        eduStudentEnrollPreOfflineService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollPreOffline0006", title = "线下新生登记根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "eduStudentEnrollPreOffline0006",
            operationName = "线下新生登记删除",
            dataType = "studentEnrollPreId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduStudentEnrollPreOfflineKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getStudentEnrollPreId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduStudentEnrollPreOffline entity = eduStudentEnrollPreOfflineService.getById(vo.getStudentEnrollPreId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduStudentEnrollPreOfflineService.removeById(vo.getStudentEnrollPreId());
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollPreOffline0010", title = "线下准新生登记保存", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "eduStudentEnrollPreOffline0010",
            operationName = "线下准新生登记保存",
            dataType = "studentEnrollPreId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduStudentEnrollPreVO savePre(EduStudentEnrollPreOfflineVO vo) {
        if (ObjectUtils.isEmpty(vo.getStudentEnrollPreId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduStudentEnrollPreOffline oldEntity = eduStudentEnrollPreOfflineService.getById(vo.getStudentEnrollPreId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }


        //保存数据
        oldEntity.setAuthRemarks(vo.getAuthRemarks());
        oldEntity.setAuthStatus(vo.getAuthStatus());
        eduStudentEnrollPreOfflineService.updateById(oldEntity);


        if(AuthStatusEnum.YES.getType().equals(vo.getAuthStatus())){
            EduStudentEnrollPreVO  enrollPreVO = Convert.convert(EduStudentEnrollPreVO.class,oldEntity);
            enrollPreVO.setEnrollPreType(EnrollPreTypeEnum.gl.getType());
            enrollPreVO.setType(SaveTypeEnum.INSERT.getType());
            enrollPreVO.setManagerFlag(true);
            enrollPreVO.setRemarks("线下登记");
            return eduStudentEnrollPreIntegration.add(enrollPreVO);
        }else {
            return null;
        }

    }
}
